Config.bInit();

var navbar = $( '#navbar' );
var searchBox = $( '#search-field' );
var basicSections = $( '.displaytable > section' );

$( '.navbar-item' ).forEach( function( li ) {
	$.extend( li, {
		'nav' : function() {
			'use strict';
			var thisLi = $( this );
			if ( thisLi.hasClass( 'navbar-item-selected' ) )
				return;
			thisLi.siblings().removeClass( 'navbar-item-selected' );
			thisLi.addClass( 'navbar-item-selected' );

			var name = thisLi.attr( 'pagename' );
			$('.page').forEach(function( page ) {
				if ( page.id !== name )
					page.setAttribute( 'hidden', '' );
				else
					page.removeAttribute( 'hidden' );
			});
		}
	});
});

navbar.click( function(e) {
	'use strict';

	var target = $( e.target );
	if ( !target.hasClass( 'navbar-item' ) )
		return;
	if ( target.attr( 'id' ) === 'searchPageNav' )
		return;
	clearSearch();
	target[0].nav();
	searchBox.val( '' );
	if ( target.attr( 'id' ) === 'aboutPageNav' && $( '.user-panel' ).attr( 'src' ) === 'about:blank' ) {
		setTimeout( function() {
			$( '.user-panel' ).attr( 'src', 'http://tieba.baidu.com/i/data/panel?un=%D3%C6%D3%C6%C4%CE%BA%CE&ref=bdb-option-page' );
		}, 250 );
	}
});
searchBox.input( function( e ) {
	if ( searchBox.val() ) {
		search( searchBox.val() );
	} else {
		clearSearch();
		$( '#stylePageNav' )[0].nav();
	}
} );

// Init page.
$( '#stylePageNav' )[0].nav();


function search( word ) {
	'use strict';
	word = word.toUpperCase();
	if ( $( '#searchPage' ).attr( 'searching-word' ) === word )
		return;
	searchBox.parent()[0].nav();

	if ( word === '悠悠奈何' && $( '.user-panel' ).attr( 'src' ) === 'about:blank' )
		$( '.user-panel' ).attr( 'src', 'http://tieba.baidu.com/i/data/panel?un=%D3%C6%D3%C6%C4%CE%BA%CE&ref=bdb-option-page' );

	var displaytable = $( '#searchPage > .displaytable' );

	var frag = document.createDocumentFragment(), found = false;
	for ( var sec, i = 0, l = basicSections.length; i <  l; i++ ) {
		sec = basicSections[i];
		if ( sec.textContent.toUpperCase().indexOf(word) > -1 ) {
			found = true;
			frag.appendChild( sec.cloneNode( true ) );
		}
	}

	if ( !found ) {
		displaytable.html( '' );
		$( '<div>' ).attr( 'id', 'searchPageNoMatches' ).html( '<p>未找到匹配项。</p>' ).appendTo( displaytable );
		$( '#searchPage' ).attr( 'searching-word', word );
		return;
	} else {
		displaytable.html( '' ).append( frag );
		$( '#searchPage' ).attr( 'searching-word', word );
	}

	// Highlight the search word.
	$( '*', displaytable ).forEach( function( node ) {
		node = $( node );
		if ( node.children().length || node.text().toUpperCase().indexOf( word ) === -1 )
			return;
		node.html(
			node.html().replace( new RegExp( word, 'ig' ), function( w ) {
				return '<span class="search-highlighted">' + w + '</span>';
			})
		);
	});

	// CloneNode doesn't clone textareas' or selects' values.
	// We need to do it manually.
	if ( $( 'select, textarea', displaytable ).length ) {
		$( 'select, textarea', displaytable ).forEach( function( elem ) {
			elem.value = $( '#' + elem.id ).val();
		});
	}
	// CloneNode doesn't clone eventListeners.
	if ( $('[bindto]', displaytable).length ) {
		var bindingOptions = $( '[bindto]', displaytable );
		bindingOptions.forEach( function( bindingOption ) {
			'use strict';
			bindingOption = $( bindingOption );

			if ( !bindingOption.attr('bindto') )
				return;
			var clonedOption = $( '#' + bindingOption.attr( 'id' ) );
			var bindedOption = $( '#' + bindingOption.attr( 'bindto' ), displaytable );
			bindedOption.click( function( e ) {
				if ( this.checked === false ) {
					bindingOption.attr( 'disabled', '' );
					clonedOption.attr( 'disabled', '' );
				} else {
					bindingOption.removeAttr( 'disabled' );
					clonedOption.removeAttr( 'disabled' );
				}
			} );
		});
	}

}


function clearSearch() {
	$( '#searchPage > .displaytable' ).html( '' ).attr( 'searching-word', '' );
}

